<template>
  <!-- 三级列表 -->
  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <el-form :inline="true"  class="demo-form-inline" :disabled="disabled">
      
      <el-form-item label="一级分类">
        <!-- 
            在select中，如果想要显示option，select必须有双向绑定一个值，
            值是下边所有option的某个一个value属性值（收集的是value属性值 
        -->
        <el-select  placeholder="请选择" v-model="(category1Id as number)" ref="SelectEle" @change="changeCategory1">
          <el-option
            v-for="item in categoryList1"
            :label="item.name"
            :value="item.id"
            :key="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select  placeholder="请选择" v-model="(category2Id as number)" @change="changeCategory2">
          <el-option
            v-for="item in categoryList2"
            :label="item.name"
            :value="item.id"
            :key="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select  placeholder="请选择" v-model="(category3Id as number)">
          <el-option
            v-for="item in categoryList3"
            :label="item.name"
            :value="item.id"
            :key="item.id"
          />
        </el-select>
      </el-form-item>

    </el-form>
  </el-card>
  
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Category",
});
</script>

<script setup lang="ts">
//引入仓库
import useAttrStore from "@/stores/attr"
import { storeToRefs } from "pinia";
import {onMounted} from "vue"

defineProps<{
  disabled: boolean
}>()

//得到仓库实例
const attrStore = useAttrStore()
//解构出方法和数据
const {
  categoryList1, 
  category1Id, 
  categoryList2,
  category2Id,
  categoryList3,
  category3Id,
} = storeToRefs(attrStore)

//1.初始化请求一级列表数据
onMounted(() => {
  //调用store中的方法,发送1级分类请求
  attrStore.getCategory1();
});


//2.一级列表改变的回调
const changeCategory1 = () => {
  //点击一级列表之前先清空二三级的数据
  categoryList2.value = []
  category2Id.value = null
  categoryList3.value = []
  category3Id.value = null
  //发送二级列表请求
  attrStore.getCategory2();
}

//3.一级列表改变的回调
const changeCategory2 = () => {
  //点击一级列表之前先清空三级的数据
  categoryList3.value = []
  category3Id.value = null
  //发送三级列表请求
  attrStore.getCategory3();
}
</script>
